package top.lanscarlos.ashcraft.controller;

import com.google.gson.JsonObject;
import com.sun.istack.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.lanscarlos.ashcraft.pojo.User;
import top.lanscarlos.ashcraft.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

/**
 * Ashcraft
 * top.lanscarlos.ashcraft.controller
 *
 * @author Lanscarlos
 * @since 2022-05-25 14:11
 */
@RestController
@RequestMapping("/user")
/* 跨域问题处理 */
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class UserController extends AshController {
    @Autowired
    private UserService service;

    @RequestMapping("/register")
    public Object register(
//            JsonObject json,
            HttpServletRequest request,
            @NotNull String name,
            @NotNull String password,
            @Nullable MultipartFile avatar,
            @Nullable String signature,
            int gender,
            @Nullable String address,
            @NotNull String phone,
            double money
    ) {
        String avatar_path = handleMultipartFile(request.getServletContext(), avatar);
        User user = service.register(name, password, avatar_path, signature, gender, address, phone, money);
//        if (user != null) {
//            json.addProperty("result", true);
//            json.add("user", toJson(user));
//        } else {
//            json.addProperty("result", false);
//        }
        return toJson(user);
    }
}
